package mx.vlabs.soapdroid.wsdl2java.artifactbuilder;

import static mx.vlabs.soapdroid.utils.Utils.*;

import java.util.HashMap;
import java.util.Map;

import mx.vlabs.soapdroid.wsdl2java.Artifact;

import org.ow2.easywsdl.wsdl.api.Endpoint;
import org.ow2.easywsdl.wsdl.api.InterfaceType;
import org.ow2.easywsdl.wsdl.api.Service;
import org.ow2.easywsdl.wsdl.api.WSDLException;

public class ServiceArtifactBuilder extends ArtifactBuilder {
	
	private Service service;
	private String wsdl;
	

	public ServiceArtifactBuilder(Service service,String packageName,String wsdl){
		super(packageName);
 		this.service = service;
 		this.wsdl = wsdl;
	}

	public String build() {
		// TODO Auto-generated method stub
		String serviceClassName = capitalizeFirstLetter(service.getQName().getLocalPart());
		getArtifact().setName(serviceClassName);
		
		getArtifact().setParam("serviceClassName", serviceClassName);
		getArtifact().setParam("wsdlLocation", wsdl);
		
		buildPortTypeData();
		return getArtifact().getContent();
	}
	
	public void buildPortTypeData(){
				
		
		
		InterfaceType interfaceType;
		//service.geten
		
		StringBuilder sbQNames  = new StringBuilder();
		StringBuilder sbGetters = new StringBuilder();
		try {
			interfaceType = service.getInterface();
			
			sbQNames.append(readPortTypeQName(interfaceType));
			sbGetters.append(readPortTypeGetters(interfaceType));
			
		} catch (WSDLException e) {
			// TODO Auto-generated catch block
			for(Endpoint endpoint : service.getEndpoints()){
				sbQNames.append("\n");
				sbQNames.append(readPortTypeQName(endpoint));
				sbGetters.append("\n");
				sbGetters.append(readPortTypeGetters(endpoint));
				
			}
		}
		
		
		
		getArtifact().setParam("portTypeQNames", sbQNames.toString());
		getArtifact().setParam("servicePortGetters", sbGetters.toString());
		
	}
	
	private String readPortTypeQName(Object portType){
		
		Artifact portTypeArtifact = getArtifactLoader().load("service.portname.constant", getArtifactType());
		
		
		Map<String,String> portTypeData = readPortTypeData(portType);
		

		portTypeArtifact.setParam("portTypeClassName", portTypeData.get("portTypeClassName"));
		portTypeArtifact.setParam("portTypeNamespace", portTypeData.get("portTypeNamespace"));
		portTypeArtifact.setParam("portTypeName", portTypeData.get("portTypeName"));
		
		
		return portTypeArtifact.getContent();
		
	}
	
	private String readPortTypeGetters(Object portType){
		
		Artifact portTypeArtifact = getArtifactLoader().load("service.port.getter", getArtifactType());
		
		Map<String,String> portTypeData = readPortTypeData(portType);
		
		portTypeArtifact.setParam("portTypeClassName", portTypeData.get("portTypeClassName"));
		
		return portTypeArtifact.getContent();
	}
	
	private Map<String,String> readPortTypeData(Object portType){
		
		Map<String,String> map = new HashMap<String, String>();
		
		String portTypeNamespace = "";
		String portTypeName	     = "";
		String portTypeClassName = "";
		
		if(portType instanceof InterfaceType){
			
			InterfaceType interfaceType = (InterfaceType) portType;
		
			portTypeNamespace =  interfaceType.getQName().getNamespaceURI();
			portTypeName	     = interfaceType.getQName().getLocalPart();
			portTypeClassName = capitalizeFirstLetter(portTypeName);
		
		}else if(portType instanceof Endpoint){
			
			Endpoint endpoint = (Endpoint) portType;
			
			portTypeNamespace =  endpoint.getBinding().getQName().getNamespaceURI();
			portTypeName	     = endpoint.getBinding().getQName().getLocalPart();
			portTypeClassName = capitalizeFirstLetter(portTypeName);
			
		}
		
		map.put("portTypeNamespace", portTypeNamespace);
		map.put("portTypeName", portTypeName);
		map.put("portTypeClassName", portTypeClassName);
		
		return map;
	}
	

	public String getArtifactType() {
		// TODO Auto-generated method stub
		return "service";
	}

}
